<?php
/**
 * Description: 获取题目统计信息
 * User: devforma
 * Date: 15/11/27
 * Time: 18:51
 */

class GetQuestionSummary implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;

        if (empty($queryArgs['question_id']) || !filter_var($queryArgs['question_id'], FILTER_VALIDATE_INT)
            || $queryArgs['terminal_type'] == 'iOS_phone') {
            return genOutput(1000);
        }

        $row = $cmsdb->table($cmsdb->questionQuality)
            ->select('test_number', 'correct_number', 'answer_summary')
            ->where(array('question_id'=>$queryArgs['question_id'], 'testarea_id'=>0))
            ->first();

        if (!$row)
            return genOutput(1000);

        $data = array(0,'0.0','');
        if ($row->test_number != 0) {
            $answers = json_decode($row->answer_summary, true);
            if (is_array($answers)) {
                $rightAnswer = $cmsdb->table($cmsdb->oZtkQuestions)
                    ->where('id', $queryArgs['question_id'])
                    ->value('answer');
                $max = 0;
                $fallible = '';
                foreach ($answers as $k => $v) {
                    if (strcmp($k, $rightAnswer) === 0)
                        continue;

                    if ($max <= $v) {
                        $max = $v;
                        $fallible = $k;
                    }
                }

                $data = array('count' => intval($row->test_number),
                    'accuracy' => round($row->correct_number / $row->test_number, 3),
                    'fallible' => $fallible
                );
            }
        }

        return genOutput(1, $data);
    }
}